package com.echoesnet.echoesshortvideo.Repositories;

import android.arch.lifecycle.LiveData;
import android.arch.lifecycle.MutableLiveData;
import android.content.ContentResolver;
import android.database.Cursor;
import android.provider.MediaStore;

import com.echoesnet.echoesshortvideo.AppController;
import com.echoesnet.echoesshortvideo.Persistence.VideoPathBean;

import java.io.File;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;

/**
 * Copyright (C) 2017 在线回声（天津）科技发展有限公司
 * 在线回声完全享有此软件的著作权，违者必究
 *
 * @author an
 * @version 1.0
 * @modifier
 * @createDate 2018/4/23 0023
 * @description
 */
public class VideoSelectRepository
{
    private MutableLiveData<List<VideoPathBean>> data;

    public LiveData<List<VideoPathBean>> getVideoList()
    {
        if (data == null)
            data = new MutableLiveData<>();
        Observable.create(new ObservableOnSubscribe<List<VideoPathBean>>()
        {
            @Override
            public void subscribe(ObservableEmitter<List<VideoPathBean>> emitter) throws Exception
            {
                ContentResolver mContentResolver = AppController.getInstance().getContentResolver();
                Cursor mCursor = mContentResolver.query(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, null,
                        null,
                        null,
                        null);
                File file;
                List<VideoPathBean> videoList = new ArrayList<>();
                while (mCursor.moveToNext())
                {
                    VideoPathBean videoPathBean = new VideoPathBean();
                    // 获取图片的路径
                    String path = mCursor.getString(mCursor.getColumnIndex(MediaStore.Images.Media.DATA));
                    if (path.endsWith(".mp4"))
                    {
                        long duration = mCursor.getInt(mCursor.getColumnIndex(MediaStore.Video.Media.DURATION));
                        if (duration > 0)
                        {
                            videoPathBean.setVideoPath(path);
                            videoPathBean.setDurantion(duration);
                            file = new File(path);
                            if (file.exists())
                                videoPathBean.setTime(file.lastModified());
                            videoList.add(videoPathBean);
                        }
                    }
                }
                mCursor.close();
                Collections.sort(videoList, new Comparator<VideoPathBean>() {
                    @Override
                    public int compare(VideoPathBean o1, VideoPathBean o2) {
                        if(o1 == null && o2 == null) {
                            return 0;
                        }
                        if(o1 == null) {
                            return -1;
                        }
                        if(o2 == null) {
                            return 1;
                        }
                        if (o1.getTime() == o2.getTime())
                            return 0;
                        return o1.getTime() >  o2.getTime() ? -1 : 1;
                    }
                });
                emitter.onNext(videoList);
            }
        }).subscribeOn(Schedulers.computation())
                .unsubscribeOn(Schedulers.computation())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<List<VideoPathBean>>()
                {
                    @Override
                    public void accept(List<VideoPathBean> videoPathBeans) throws Exception
                    {
                        data.setValue(videoPathBeans);
                    }
                });
        return data;
    }
}
